package com.prawn.ift_collequipment.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.prawn.ift_collequipment.pojo.EquipmentStatus;
import com.prawn.ift_collequipment.service.EquipmentStatusService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import util.IdWorker;
import util.NullAttributeUtils;

/**
 * 设备状态管理
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/equipmentStatus")
public class EquipmentStatusController {

	@Autowired
	private EquipmentStatusService equipmentStatusService;

	@Autowired
	private IdWorker idWorker;

	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true,StatusCode.OK,"查询成功",equipmentStatusService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",equipmentStatusService.findById(id));
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<EquipmentStatus> pageList = equipmentStatusService.findSearch(searchMap, page, size);
		List<EquipmentStatus> equipmentStatuses=new ArrayList<>();
		for (EquipmentStatus status:pageList.getContent()){
			if(new Date(System.currentTimeMillis()).getTime()-status.getCommunicationTime().getTime()>1800000) {//半小时
				if (status.getRunningState()!=null && !"".equals(status.getRunningState())&&status.getRunningState().equals("1")) {
					status.setRunningState("0");
					equipmentStatuses.add(status);
				}
			}
		}
		rabbitTemplate.convertAndSend("equipmentStatus","changeStatus.changeRunning",equipmentStatuses);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<EquipmentStatus>(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",equipmentStatusService.findSearch(searchMap));
    }
	
	/**
	 * 增加
	 * @param equipmentStatus
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody EquipmentStatus equipmentStatus){
		equipmentStatus.setId(String.valueOf(idWorker));
		equipmentStatus.setCreateDate(new Date());
		equipmentStatus.setUpdateDate(new Date());
		equipmentStatusService.add(equipmentStatus);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param equipmentStatus
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody EquipmentStatus equipmentStatus, @PathVariable String id ){
		EquipmentStatus database=equipmentStatusService.findById(id);
		database.setUpdateDate(new Date());
		equipmentStatus.setId(id);
		BeanUtils.copyProperties(equipmentStatus,database, NullAttributeUtils.getNullPropertyNames(equipmentStatus));
		equipmentStatusService.update(database);
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		equipmentStatusService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}
	
}
